<?php
function get_template ($template_name = 'index') {
  global $module;
  if (file_exists(ROOT . '/content/theme/' . THEME . '/' . $module . '/' . $template_name . '.php')) {
    return ROOT . '/content/theme/' . THEME . '/' . $module . '/' . $template_name . '.php';
  } else if (file_exists(ROOT . '/content/theme/default/' . $module . '/' . $template_name . '.php')) {
    return ROOT . '/content/theme/default/' . $module . '/' . $template_name . '.php';
  } else {
    die('template not found!');
  }
}

function get_partial($partial_name) {
  global $module;
  #return ROOT . '/content/theme/' . $theme . '/' . $partial_name . '.php';
  if (file_exists(ROOT . '/content/theme/' . THEME . '/' .  $partial_name . '.php')) {
    return ROOT . '/content/theme/' . THEME . '/' . $partial_name . '.php';
  } else if (file_exists(ROOT . '/content/theme/default/' . $partial_name . '.php')) {
    return ROOT . '/content/theme/default/' . $partial_name . '.php';
  } else {
    die('partial not found!');
  }
}

// url => $_GET
/*
function parse_url () {
}
*/

// 
function url($url = '', $encode = true) {
  if ($encode) {
    return str_replace('&', '&amp;', $url);
  } else {
    return $url;
  }
}

function get_categories_by_parent_id($parent_id = '0', $status = 'visiable'){
  return db_fetch_all("
    select * 
    from category 
    where parent_id = '" . $parent_id . "' 
      and status = '" . $status . "'
    order by sort_order desc
  ");
}

function get_products_by_category_id($c_id, $status = 'visiable'){
  return db_fetch_all("
    select * 
    from product
    where category_id = '" . $c_id . "' 
      and status = '" . $status . "'
    order by sort_order desc
  ");
}
/*
function get_pages($status = 'visiable') {
  return db_fetch_all("
    select *
    from page
    where status = '" . $status . "'
    order by sort_order desc
  ");
}
*/
function get_pages_by_parent_id($parent_id = 0, $status = 'visiable') {
  return db_fetch_all("
    select *
    from page
    where status = '" . $status . "'
      and parent_id = '" . $parent_id . "'
    order by sort_order desc
  ");
}

function forward404() {
  die('forward 404');
}

function forward404unless($object) {
  if(!$object){
    forward404();
  }
}

function forward($module_name, $action_name, $parameters = array()) {
  global $_GET,$module,$action,$queries;
  $module = $module_name;
  $action = $action_name;
  if (file_exists(ROOT . '/module/' . $module . '/' . $action . '.php')) {
    foreach ($parameters as $k => $p) {
      $_GET[$k] = $p;
    }
    include ROOT . '/module/' . $module . '/' . $action . '.php';
    exit;
  } else {
    forward404();
  }
}

function redirect($url) {
  header('location:' . url($url, false));
  exit;
}
/*
function message ($msg) {
  exit($msg);
}

function error ($msg) {
  exit($msg);
}
*/
function display_category_tree($parent_id = 0) {
  $categories = get_categories_by_parent_id($parent_id);
  if ($categories) {
?>
    <ul<?php if ($parent_id != 0) echo ' class="children"';?>>
<?php foreach ($categories as $category) { ?>
      <li class="cat-item cat-item-<?php echo $category['id'];?>">
        <a href="<?php echo url('?module=category&category_id=' . $category['id']);?>" title="<?php echo $category['description'];?>"><?php echo $category['name'];?></a>
        <?php display_category_tree($category['id']);?>
      </li>
<?php }?>
    </ul>
<?php
  }
}

function display_page_tree($parent_id = 0) {
  $pages = get_pages_by_parent_id($parent_id);
  if ($pages) {
?>
    <ul<?php if ($parent_id != 0) echo ' class="children"';?>>
<?php foreach ($pages as $page) { ?>
      <li class="page-item page-item-<?php echo $page['id'];?>">
        <a href="<?php echo url('?module=page&page_id=' . $page['id']);?>" title="<?php echo $page['title'];?>"><?php echo $page['title'];?></a>
        <?php display_page_tree($page['id']);?>
      </li>
<?php }?>
    </ul>
<?php
  }
}
/*
function is_logined () {
  return isset($_SESSION['isLogined']) && $_SESSION['isLogined'] === true;
}
*/

  function get_product_images($product_id) {
    return db_fetch_all("select * from product_image where product_id='" . $product_id . "'");
  }